package org.yx.service.smat.release_material_sn;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.yx.exception.BizException;
import org.yx.models.SmatMsnSts;
import org.yx.models.SqcmRlsDef;
import org.yx.service.bas.create_rule_code.BasCreateRuleCodeService;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryIn;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryService;
import org.yx.service.smat.view_held_material_sn.HeldMsn;
import org.yx.service.smat.view_held_material_sn.SmatViewHeldMaterialSnIn;
import org.yx.service.smat.view_held_material_sn.SmatViewHeldMaterialSnService;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;
import org.yx.utils.ServiceBeanUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料批次解除锁定
 * @author zxw
 * @since 2018/05/26
**/
public class SmatReleaseMaterialSnService extends BaseService {
	private final static String RELEASE_NO = "MSN_RELEASE_NO";

	public SmatReleaseMaterialSnOut releaseMaterialSn(SmatReleaseMaterialSnIn in) throws Exception {
		if (StrUtil.isAllBlank(in.getMsn(), in.getMatCode(), in.getHoldNo())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "msn,matCode,holdNo"));
		}
		if (StrUtil.isBlank(in.getReleaseCode())) {
			//throw new BizException("SYS-0004", new FieldMap("parameter", "releaseCode"));
		}

		SmatViewHeldMaterialSnIn snIn = new SmatViewHeldMaterialSnIn();
		copyProperties(in, snIn);
		List<HeldMsn> msnList = ServiceBeanUtil.getBean(SmatViewHeldMaterialSnService.class).viewHeldMaterialSn(snIn)
				.getHeldMsnList();
		if (CollUtil.isEmpty(msnList)) {
			throw new BizException("SMAT-0004");//没有可用的msn
		}

		String releaseNo = BasCreateRuleCodeService.createRuleCode(RELEASE_NO);
		List<SmatMsnSts> msnStsList = new ArrayList<SmatMsnSts>();
		for (HeldMsn heldMsn : msnList) {
			if (!StrUtil.equals(heldMsn.getHoldPwd(), in.getHoldPwd())) {
				throw new BizException("SQCM-0009", new FieldMap("holdNo", heldMsn.getHoldNo()));
			}

			SmatMsnSts msnSts = new SmatMsnSts();
			msnSts.setOrgCode(heldMsn.getOrgCode());
			msnSts.setMsn(heldMsn.getMsn());
			msnSts.setStatus('C');
			msnSts.setEventCode("RELEASE");
			msnSts.setEventValue(releaseNo);
			msnSts.setReleaseNo(releaseNo);
			msnSts.setHoldNo(null);

			msnStsList.add(msnSts);
		}

		updateBatch(msnStsList, "status,eventCode,eventValue,releaseNo,holdNo");

		SmatInsertMaterialSnHistoryIn msnHisIn = new SmatInsertMaterialSnHistoryIn();
		msnHisIn.setMsnStsList(msnStsList);
		ServiceBeanUtil.getBean(SmatInsertMaterialSnHistoryService.class).insertMaterialSnHistory(msnHisIn);

		SqcmRlsDef release = new SqcmRlsDef();
		release.setOrgCode(in.getUserOrgCode());
		release.setReleaseNo(releaseNo);
		release.setReleaseType('M');
		release.setHoldNo(msnList.get(0).getHoldNo());
		release.setReleaseTime(getCurrentTime());
		release.setReleaseUser(in.getUserToken().getUserId());

		insert(release);

		SmatReleaseMaterialSnOut out = new SmatReleaseMaterialSnOut();
		out.setReleaseNo(releaseNo);

		return out;
	}
}